﻿using System;
using System.Collections.Generic;
using System.Text;


using CMN.Common;
using CMN.Entity;
using CMN.BLL;
using CMN.Cache;
using CMN.Extends;
using System.IO;
using CMN.Framework;
using System.Data.Common;
using CMN.DBUtility;
namespace CMN.Extends.Job
{
    public class JobSaveClickRecord : IJob
    {
        /// <summary>
        /// 执行任务
        /// </summary>
        /// <param name="obj"></param>
        public void Execute(object obj)
        {




            Normal_ClickRecordEntity[] clickRecordNew = null;
            Module_ArticleEntity[] articleNew = null;
            try
            {
                List<Normal_ClickRecordEntity> clickRecord = MvcWebCacheManager.GetNormal_ClickRecordList();
                if (clickRecord != null)
                {
                    clickRecordNew = new Normal_ClickRecordEntity[clickRecord.Count];
                    clickRecord.CopyTo(clickRecordNew, 0);
                    clickRecord.Clear();
                }
                List<Module_ArticleEntity> articleList = MvcWebCacheManager.GetCacheModule_ArticleSearchList();
                if (articleList != null)
                {
                    articleNew = new Module_ArticleEntity[articleList.Count];
                    articleList.CopyTo(articleNew, 0);
                    articleList.Clear();
                }
                saveCache(clickRecordNew, articleNew);
                //AppLog.Write("更新点击数" + clickRecord.Count.ToString(), AppLog.LogMessageType.Debug);
               
            }
            catch (Exception ex)
            {
                AppLog.Write("记录点击数写入数据库报错" + ex.Message, AppLog.LogMessageType.Error);
            }
            finally
            {
                MvcWebCacheManager.RemoveNormal_ClickRecordList();
                MvcWebCacheManager.RemoveModule_ArticleSearchList();
            }

               
        }

        private void saveCache(Normal_ClickRecordEntity[] clickRecord, Module_ArticleEntity[] articleRecord)
        {

            List<CommandInfo> comdList = new List<CommandInfo>();
            Normal_ChannelEntity channelEntity = null;
            BLLHelper bll = new BLLHelper();
            //保存点击缓存
            if (clickRecord != null)
                for (int i = 0; i < clickRecord.Length; i++)
                {
                    channelEntity = MvcWebCacheManager.GetNormal_ChannelByPath(clickRecord[i].ModuleType);
                    if (channelEntity != null)
                    {
                        CommandInfo info = new CommandInfo();
                        info.CommandNextType = CommandNextType.ExcuteEffectRows;
                        info.CommandText = "Insert into Normal_ClickRecord ( Ip,AddDate,ModuleType,id )VALUES(@Ip,@AddDate,@ModuleType,@Id)";
                        info.CommandType = System.Data.CommandType.Text;
                        DbParameter[] prams;
                        prams = new DbParameter[4];
                        prams[0] = bll.GetDalHelper().CreateInDbParameter("Ip", MyDBType.Varchar, clickRecord[i].Ip);//IP地址 
                        prams[1] = bll.GetDalHelper().CreateInDbParameter("AddDate", MyDBType.DateTime, clickRecord[i].AddDate);//添加时间 
                        prams[2] = bll.GetDalHelper().CreateInDbParameter("ModuleType", MyDBType.Varchar, clickRecord[i].ModuleType);//文章类型 
                        prams[3] = bll.GetDalHelper().CreateInDbParameter("Id", MyDBType.Int, clickRecord[i].Id);//文章Id 
                        info.Parameters = prams;
                        comdList.Add(info);
                        CommandInfo info2 = new CommandInfo();
                        info2.CommandText = string.Concat("update ", "Module_", channelEntity.ChannelType, " set ViewNum=ViewNum+1  where id=", clickRecord[i].Id.ToString());
                        info2.CommandType = System.Data.CommandType.Text;
                        info2.CommandNextType = CommandNextType.None;
                        info2.Parameters = null;
                        comdList.Add(info2);
                    }

                }


            //保存内容搜索列表缓存
            if (articleRecord != null)
                for (int i = 0; i < articleRecord.Length; i++)
                {

                    CommandInfo info = new CommandInfo();
                    info.CommandNextType = CommandNextType.ExcuteEffectRows;
                    info.CommandText = "UPDATE Module_ArticleContent SET SearchUpdateTime=?SearchUpdateTime, SearchContent=?SearchContent  WHERE (Id=?Id)";
                    info.CommandType = System.Data.CommandType.Text;
                    DbParameter[] prams;
                    prams = new DbParameter[3];

                    prams[0] = bll.GetDalHelper().CreateInDbParameter("Id", MyDBType.Int, articleRecord[i].Id);//文章ID 
                    prams[1] = bll.GetDalHelper().CreateInDbParameter("SearchUpdateTime", MyDBType.DateTime, articleRecord[i].SearchUpdateTime);//频道ID 
                    prams[2] = bll.GetDalHelper().CreateInDbParameter("SearchContent", MyDBType.Text, articleRecord[i].SearchContent);//栏目ID 
                    info.Parameters = prams;
                    comdList.Add(info);
                }
            if (comdList.Count > 0)
                bll.GetDalHelper().ExecuteSqlTran(comdList);
        }
        
        public void Error(Exception e)
        {
            AppLog.Write("更新缓存出错", AppLog.LogMessageType.Error, e);
        }
    }
}
